<template>
  <div class="countdown-demo">
    <h1>倒计时组件示例</h1>
    <div class="demo-container">
      <CountDown :targetTime="targetTime" :onFinish="handleCountdownFinish" />
    </div>
    <div class="demo-controls">
      <button @click="addOneDay">增加一天</button>
      <button @click="resetCountdown">重置倒计时</button>
    </div>
    <div v-if="isFinished" class="finish-message">
      倒计时已结束！
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import CountDown from '../components/common/CountDown/index.vue';

// 倒计时目标时间（默认为当前时间 + 1天）
const targetTime = ref(new Date(Date.now() + 24 * 60 * 60 * 1000).toISOString());
const isFinished = ref(false);

// 倒计时结束的回调函数
const handleCountdownFinish = () => {
  isFinished.value = true;
  console.log('倒计时结束！');
};

// 增加一天的时间
const addOneDay = () => {
  const currentTarget = new Date(targetTime.value).getTime();
  targetTime.value = new Date(currentTarget + 24 * 60 * 60 * 1000).toISOString();
  isFinished.value = false;
};

// 重置倒计时（设置为当前时间 + 1天）
const resetCountdown = () => {
  targetTime.value = new Date(Date.now() + 24 * 60 * 60 * 1000).toISOString();
  isFinished.value = false;
};
</script>

<style scoped>
.countdown-demo {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
  text-align: center;
}

h1 {
  margin-bottom: 30px;
  color: #333;
}

.demo-container {
  margin: 40px 0;
  padding: 20px;
  background-color: #f8f9fa;
  border-radius: 10px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}

.demo-controls {
  margin-top: 20px;
  display: flex;
  justify-content: center;
  gap: 10px;
}

button {
  padding: 8px 16px;
  background-color: #6b46c1;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s;
}

button:hover {
  background-color: #553c9a;
}

.finish-message {
  margin-top: 20px;
  padding: 10px;
  background-color: #f8d7da;
  color: #721c24;
  border-radius: 4px;
  font-weight: bold;
}
</style>